<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/1/1
 * Time: 上午9:39
 */

class Point_model extends Base_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function checkUserComplish($uid,$courseid)
    {
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        //启发式提问第一期数据makepoint.complished
        $iscomplished=$redis->getBit('qfpoint:'.$courseid.':complished',$uid);

        if ($iscomplished===1){
            return array("status"=>2,"uid"=>$uid);
        }else{
            $info = $this->getGroupInfo($courseid);
            return array("status"=>1,"uid"=>$uid,"info"=>$info);
        }
    }

    public function getGroupInfo($courseid)
    {
        $this->load->database();
        $info = $this->db->select('id,name,member')->from('mod_qf_group')
            ->where('course',$courseid)->order_by('sort','asc')->get()->result_array();

        $pointarr = array();
        for ($i=1;$i<count($info)+1;$i++){
            $pointarr[]=$i;
        }

        $res = array();
        foreach ($info as $row){
            $resrow=array();
            $resrow['name']=$row['name'];
            $resrow['gid']=$row['id'];
            $resrow['member']=$row['member'];
            $resrow['point']=$pointarr;
            $res[]=$resrow;
        }
        return $res;
    }

    public function submitAns($uid,$points,$courseid)
    {
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        $redis->setBit('qfpoint:'.$courseid.':complished',$uid,1);
        foreach ($points as $gid=>$p){
            $gid=(int)$gid+1;
            //启发式提问第一期数据makepoint.$gid
            $redis->zAdd('qfpoint:'.$courseid.':'.$gid,$p,$uid);
        }
    }

    public function getStatis($courseid)
    {
        $this->load->database();
        $groupdata = $this->db->select('id,name,member')->
        from('mod_qf_group')->where('course',$courseid)->order_by('sort','asc')->get()->result_array();

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();

        $res = array();
        $totalvote = $this->db->select('num')->from('mod_qf_info')->where('id',$courseid)->get()->row_array();
        $res['totalvote'] = $totalvote['num'];
        $vote = $redis->bitCount('qfpoint:'.$courseid.':complished');
        $res['vote'] = $vote;

        $maxpoints = 0;
        foreach ($groupdata as $g) {
            $resrow=array();
            $points=$redis->zRange('qfpoint:'.$courseid.':'.$g['id'],0,-1,true);
            $total = 0;
            foreach ($points as $p ){
                $total+=(int)$p;
            }
            if ($total>$maxpoints){
                $maxpoints=$total;
            }
            $resrow['group']=$g['name'];
            $resrow['score']=$total;
            $res['data'][]=$resrow;
        }

        foreach ($res['data'] as &$row) {
            $row['height']=$maxpoints>0?round($row['score']/$maxpoints*500,2):0;
        }

        return $res;
    }

}